package org.mzy.serve.service.impl;

import org.mzy.basic.config.BussinessException;
import org.mzy.basic.service.impl.BaseServiceImpl;
import org.mzy.basic.util.BaiduAuditUtils;
import org.mzy.serve.domain.Product;
import org.mzy.serve.domain.ProductDetail;
import org.mzy.serve.domain.ProductOnlineAuditLog;
import org.mzy.serve.mapper.ProductDetailMapper;
import org.mzy.serve.mapper.ProductMapper;
import org.mzy.serve.mapper.ProductOnlineAuditLogMapper;
import org.mzy.serve.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzy
 * @since 2022-11-03
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    public void onsale(List<Long> ids) {
        // 遍历id集合,获取product数据
        for (Long id : ids) {
            Product product = productMapper.findById(id);
            // new一个String来存数据
            StringBuffer stringBuffer = new StringBuffer();
            // 如果审核名字不合规，则
            if (!BaiduAuditUtils.TextCensor(product.getName())) {
                stringBuffer.append("产品名称不合规" + product.getName() + ";");
            }
            // 如果图片为空，就抛异常
            if (product.getResources() == null) {
                throw new BussinessException("图片不合规");
            }
            // 如果有两张图片，则按逗号分开
            if (product.getResources().split(",").length > 1) {
                if (!BaiduAuditUtils.ImgCensor(product.getResources().split(",")[0])) {
                    stringBuffer.append("宠物图片不合规" + product.getResources() + ";");
                }
            } else {
                // 如果审核图片不合规，也抛异常
                if (!BaiduAuditUtils.ImgCensor(product.getResources())) {
                    stringBuffer.append("宠物图片不合规" + product.getResources() + ";");
                }
            }
            // 如果stringBuffer的长度大于1，说明有内容不合规，就跳过此数据
            if (stringBuffer.length() > 0) {
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(id);
                // 保存日志状态为失败
                log.setState(0);
                // 日志文本为异常文本
                log.setNote(stringBuffer.toString());
                log.setAuditTime(new Date());
                log.setAuditId(333l);
                productOnlineAuditLogMapper.add(log);
                continue;
            }
            productMapper.onsale(id);
//            FreemarkUtil.getHtml(id);
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        // 调用下架方法
        productMapper.offsale(ids);
    }

    @Override
    public Product getProductDetail(Long id) {
        return productMapper.getProductDetail(id);
    }

    @Override
    public void add(Product product) {
        productMapper.add(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.add(productDetail);
        }
    }
}
